package com.ly.mpayment.persist.service.callorder;

import com.alipay.api.domain.Car;
import com.ly.mpayment.core.util.StringUtils;
import com.ly.mpayment.persist.entity.CardOrder;
import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ICardOrderServiceImpl extends AbstractService<CardOrder, Serializable> implements ICardOrderService  {

    @Override
    public boolean saveOrder(CardOrder cardOrder) {
        if(cardOrder != null){
            this.save(cardOrder);
            return true;
        }
        return false;
    }

    @Override
    public boolean updateOrder(CardOrder cardOrder) {
        if(cardOrder != null && !StringUtils.isBlank(cardOrder.getId())){
            this.updateEntity(cardOrder);
            return true;
        }
        return false;
    }

    @Override
    public CardOrder findById(String id) {
        return this.findUnique(" select b from CardOrder b  where b.id = ?",id);
    }

    @Override
    public Page<CardOrder> findPage(Map<String, Object> parameters, int start, int pageSize) {
        return this.findPageByNamed("select b from CardOrder b where 1 = 1  #[and b.operatorname = :operatorname ]# " +
                " #[and b.name like :name ]# #[and b.cardNum = :cardNum ]# #[and b.upName = :upName ]# " +
                " #[and b.upDiscount = :upDiscount ]# #[and b.downName = :downName ]# " +
                " #[and b.downDiscount = :downDiscount ]# #[and b.faceValue = :faceValue ]# " +
                " #[and b.status = :status ]#  #[and b.imName = :imName ]#  #[and b.exName = :exName ]# " +
                " #[and b.createTime >= :startTime ]#  #[and b.createTime <= :endTime ]#  order by b.createTime desc", parameters, start, pageSize);
    }

    @Override
    public String importOrder(List<CardOrder> cardOrderList) {
        for (CardOrder cardOrder : cardOrderList){
            //检查卡号是否存在
            this.save(cardOrder);
        }
        return "1";
    }

    @Override
    public String exportOrder(Map<String, Object> parameters) {
        return null;
    }

    @Override
    public boolean deleteById(String id) {
        CardOrder cardOrder = this.findById(id);
        if(cardOrder == null){
            return false;
        }
        this.delete(cardOrder);
        return true;
    }

    @Override
    public Page<CardOrder> updateExportCallOrder(Map<String, Object> parameters, int start, int pageSize,String type) {
        Page<CardOrder> cardOrderPage = this.findPageByNamed("select b from CardOrder b where 1 = 1  #[and b.operatorname = :operatorname ]# " +
                " #[and b.name like :name ]# #[and b.cardNum = :cardNum ]# #[and b.upName = :upName ]# " +
                " #[and b.upDiscount = :upDiscount ]# #[and b.downName = :downName ]# " +
                " #[and b.downDiscount = :downDiscount ]# #[and b.faceValue = :faceValue ]# " +
                " #[and b.status = :status ]#  #[and b.imName = :imName ]#  #[and b.exName = :exName ]# " +
                " #[and b.createTime >= :startTime ]#  #[and b.createTime <= :endTime ]#  order by b.createTime desc", parameters, start, pageSize);
        if (type != null && type.equals("out")) {
            List<CardOrder> cardOrders = cardOrderPage.getResult();
            for (CardOrder cardOrder : cardOrders) {
                CardOrder oldCall = this.findById(cardOrder.getId());
                if (oldCall == null || oldCall.getStatus() == 2) {
                    throw new RuntimeException("已经出库,请选择入库卡密导出");
                    //logger.error(oldCall.getCardNum() + "已经出库");
                } else {
                    cardOrder.setStatus(2);
                    this.updateEntity(cardOrder);
                }
            }
        }
        return cardOrderPage;
    }
}
